package course.p4_list.s1_OneNextList;

import course.p4_list.ListTest;
import org.junit.Assert;
import org.junit.Test;

import java.util.Optional;

import static course.p4_list.util.Random.numberGenerator;


/**
 * 单向链表测试实例编写
 */
public class TestDemo implements ListTest {

    /**
     * jdk的链表实现，用来比对自实现链表的正确性
     */
    private java.util.LinkedList<Integer> rightList;


    /**
     * 测试获取链表长度的方法
     */
    @Test
    @Override
    public void LengthTest() {
        for(int i=0;i<10;i++){
            int length = (int) (Math.random()*1000);
            LinkedList linkedList = generatorOneNextList(length,1,100);
            Assert.assertEquals(rightList.size(),linkedList.size);
            Assert.assertEquals(rightList.size(),linkedList.calSize());
        }
    }

    /**
     * 测试链表插入方法
     */
    @Test
    @Override
    public void InsertTest() {
        LinkedList linkedList = generatorOneNextList(100,1,50);
        for(int i=0;i<10;i++){
            int position = (int) (Math.random()*100);
            linkedList.insert(i,position);
            rightList.add(position,i);
            Assert.assertEquals(Optional.ofNullable(rightList.get(position)),Optional.of(linkedList.get(position)));
        }
    }

    /**
     * 删除方法测试
     */
    @Test
    @Override
    public void DeleteTest() {
        LinkedList linkedList = generatorOneNextList(100,1,50);
        for(int i=0;i<10;i++){
            int position = (int) (Math.random()*50);
            linkedList.remove(position);
            rightList.remove(position);
            Assert.assertEquals(Optional.ofNullable(rightList.get(position)),Optional.of(linkedList.get(position)));
        }
    }


    /**
     * 自动随机生成单向链表，用于测试
     * @param length 链表长度
     * @param min 链表最小值
     * @param max 链表最大值
     * @return 生成的链表
     */
    public LinkedList generatorOneNextList(int length, int min, int max) {
        LinkedList linkedList = new LinkedList();
        rightList = new java.util.LinkedList<>();
        for(int i=0;i<length;i++) {
            int num = numberGenerator(min,max);
            linkedList.add(num);
            rightList.add(num);
        }
        return linkedList;
    }




}
